#include "Parent.h"

using namespace std;

//ParentDataRow
ParentDataRow::ParentDataRow(int parent, int child) {
	this->parent = parent;
	this->child = child;	
}

void ParentDataRow::setParent(int parent) {
	this->parent = parent;
}

void ParentDataRow::setChild(int child){
	this->child = child;
}

int ParentDataRow::getParent() {
	return this->parent;
}

int ParentDataRow::getChild(){
	return this->child;
}

//Parent
vector<ParentDataRow> Parent::table = vector<ParentDataRow>();

Parent::Parent() {
}

void Parent::setParentRelation(int parent, int child){
	table.push_back(ParentDataRow(parent, child));
}

bool Parent::isParentRelation(int parent, int child){
	for (size_t i = 0; i < table.size(); i++) {
		if (table[i].getParent() == parent && table[i].getChild() == child) {
			return true;
		}
	}
	return false;
}

bool Parent::isParentStarRelation(int parent, int child){
	for(size_t i = 0; i < table.size(); i ++) {
		if (table[i].getParent() == parent && table[i].getChild() == child) {
			return true;
		}
		if (table[i].getParent() == parent) {
			return isParentStarRelation(table[i].getChild(), child);
		}
	}
	return false;
}

int Parent::getParent(int child){
	for (size_t i = 0; i < table.size(); i++) {
		if (table[i].getChild() == child) {
			return table[i].getParent();
		}
	}
	return -1;
}

vector<int> Parent::getChild(int parent){
	vector<int> list;

	for (size_t i = 0; i < table.size(); i++) {
		if (table[i].getParent() == parent) {
			list.push_back(table[i].getChild());
		}
	}

	return list;
}

vector<int> Parent::getParentStar(int child){
	vector<int> list, temp;

	for(size_t i = 0; i < table.size(); i ++) {
		if (table[i].getChild() == child) {
			list.push_back(table[i].getParent());
			temp = getParentStar(table[i].getParent());
			if (temp.size() != 0) {
				list.insert(list.end(), temp.begin(), temp.end()); //push all results to list
			}
		}
	}

	//reverse(list.begin(), list.end());
	return list;
}

vector<int> Parent::getChildStar(int parent){
	vector<int> list, temp;

	for(size_t i = 0; i < table.size(); i ++) {
		if (table[i].getParent() == parent) {
			list.push_back(table[i].getChild());
			temp = getChildStar(table[i].getChild());
			if (temp.size() != 0) {
				list.insert(list.end(), temp.begin(), temp.end()); //push all results to list
			}
		}
	}

	return list;
}

//for unit testing
ParentDataRow Parent::getLastDataRowFromTable() {
	return table[table.size() - 1];
}